package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.NewMenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements NewMenuService {
@Autowired
private MenuDao menuDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page=menuDao.findPage(queryString);
        long total = page.getTotal();
        List<Menu> rows = page.getResult();
        for(Menu row:rows){
            System.out.println();
            Integer parentMenuId = row.getParentMenuId();
            String name=menuDao.findNameById(parentMenuId);
            row.setParentMenuName(name);
        }
        return new PageResult(total,rows);
    }

    @Override
    public List<String> findTopLevel() {
        List<String> list=menuDao.findTopLevel();
        return list;
    }

    @Override
    public void add(Menu menu) {
        if(menu.getParentMenuId()==null){
            menuDao.add1(menu);
        }
        else{
            menuDao.add2(menu);
        }

    }

    @Override
    public Boolean delete(Integer id) {
        Menu menu=menuDao.findChildById(id);
        if(menu==null){
            menuDao.deletezhongjian(id);
            menuDao.delete(id);

            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public List<Menu> findMenuByUsername(String username) {
        Integer id=menuDao.findUserIdByUsername(username);
        List<Integer> rolesId=menuDao.findRolesIdById(id);
        List<Menu> menus = new ArrayList<>();
        for(Integer roleId:rolesId){
            List<Integer> menusId=menuDao.findMenuIdsByRoleId(roleId);
            for(Integer menuId:menusId){
                Menu menu=menuDao.findMenuById(menuId);
                menus.add(menu);
            }
        }

        return menus;
    }

    @Override
    public Integer findParentIdByParentName(String name) {
        Integer id=menuDao.findParentIdByParentName(name);
        return id;
    }

    @Override
    public Boolean checkParentMenu(Integer id) {
       Integer number=menuDao.checkParentMenu(id);
       if(number==1){
           return true;
       }
       else{
           return false;
       }

    }

    @Override
    public String findParentNameById(Integer id) {
        String name=menuDao.findParentNameById(id);
        return name;
    }

    @Override
    public Menu findById(Integer id) {
        Menu menu=menuDao.findById(id);
        return menu;
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

}
